<template>
    <el-card class="!border-none " shadow="never">
        <el-form class="-mb-4" :model="queryFormData" label-width="98px" inline>
            
            
            <div class="grid grid-cols-4 gap-4">
                <div>
                    <el-form-item label="物流单号">
                        <el-input
                            placeholder="请输入物流单号"
                            
                            class="w-64"
                            v-model="queryFormData.logisticNumber"
                            clearable
                            @keyup.enter="resetOnePage"
                        >
                        
                        </el-input>
                    </el-form-item>
                </div>
                
                <div>
                    <el-form-item label="工单号">
                        <el-input
                            placeholder="请输入工单号"
                            
                            class="w-64"
                            v-model="queryFormData.afterSaleOrderNo"
                            clearable
                            @keyup.enter="resetOnePage"
                        >
                        
                        </el-input>
                    </el-form-item>
                </div>
                
                
                <div>
                    <el-form-item label="交易订单号">
                        <el-input
                            class="w-64"
                            placeholder="请输入用户交易订单号"
                            
                            v-model="queryFormData.orderNumber"
                            clearable
                            @keyup.enter="resetOnePage"
                        >
                        </el-input>
                    </el-form-item>
                </div>
                
                <div v-if="!showMore">
                    <el-form-item label="工单类型">
                        <el-input
                            class="w-64"
                            placeholder="请输入工单类型"
                            
                            v-model="queryFormData.afterSaleOrderType"
                            clearable
                            @keyup.enter="resetOnePage"
                        >
                        </el-input>
                    </el-form-item>
                </div>
                
                
                <div class="flex flex-row-reverse">
                    <el-form-item>
                        <div class="flex-1  flex flex-row-reverse  w-full ">
                            <el-button class="mx-2" type="success" @click="handleWaitSignList">批量签收</el-button>
                            <el-button class="mx-2" type="primary" @click="getPageLists">查询</el-button>
                            <el-button class="mx-2" @click="resetParams">重置</el-button>
                        </div>
                    
                    </el-form-item>
                </div>
            
            </div>
        
        
        </el-form>
    </el-card>
    
    
    <el-card class="!border-none mt-5" shadow="never">
        
        <el-table
            ref="tableRef"
            class="mt-2"
            size="large"
            v-loading="pagerData.loading"
            :data="pagerData.lists"
            row-key="id"
        >
            <el-table-column label="订单号" fixed prop="orderNumber" :show-overflow-tooltip="true" min-width="200"/>
            <el-table-column label="工单号" fixed prop="afterSaleOrderNo" min-width="170"/>
            
            <el-table-column label="状态" prop="status" min-width="120">
                <template #default="{ row }">
                    
                    <el-tag v-for="(item,index) in afterSaleOrderConfig.afterSaleOrderStatus"
                            v-show="row.status === item.code"
                            :key="index"
                            :disable-transitions="true"
                            :type="item.type">
                        {{ item.name }}
                    </el-tag>
                </template>
            </el-table-column>
            
            <el-table-column label="类型" prop="afterSaleOrderType" :formatter="formatterManager.formatterOrderType"
                             min-width="150">
            
            </el-table-column>
            <el-table-column label="来源" prop="source" :formatter="formatterManager.formatterReSource"
                             min-width="120"></el-table-column>
            
            
            <el-table-column label="渠道" prop="channel" min-width="120"
                             :formatter="formatterManager.formatterChannel"></el-table-column>
            
            
            <el-table-column label="店铺" prop="shopName" min-width="120"
                             :formatter="formatterManager.formatterShop"></el-table-column>
            
            
            <el-table-column label="用户名称" prop="userName" min-width="120"/>
            <el-table-column label="用户电话" prop="userPhone" min-width="150"/>
            
            <el-table-column label="寄送物流公司" prop="logisticCompany" min-width="120"/>
            <el-table-column label="寄送物流单号" fixed prop="logisticNumber" min-width="170"/>
            
            <el-table-column label="建单时间" prop="createTime " min-width="180"
                             :formatter="formatterManager.formatterCreateTime"/>
            <el-table-column label="操作人" prop="optPerson" min-width="150"/>
            
            <el-table-column label="操作" width="120" fixed="right">
                <template #default="{ row }">
                    <el-button
                        type="primary"
                        link
                        @click="handleRowDetail(row)"
                    >
                        详情
                    </el-button>
                    <el-button
                        type="primary"
                        link
                        @click="handleSignConfirm(row)"
                    >
                        签收
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        
        <pagination v-model="pagerData" @change="getPageLists"/>
    
    </el-card>
    
    
    <Detail ref="detailRef" @close="showDetailRef=false" v-if="showDetailRef"/>
    <WaitSignEdit ref="editRef" @confirm="handPopupConfirm" @close="showEditRef=false" v-if="showEditRef"/>
    <Sign ref="signRef" @confirm="handPopupConfirm" @close="showSignRef=false" v-if="showSignRef"/>

</template>

<script lang='ts' setup>

import Sign from './sign.vue'
import {usePagerManager} from "@/manager/base/PagerManager";
import Detail from '@/components/order-detail/detail.vue'
import {getAfterSaleListsByPage} from "@/api/afterSale/order/AfterSaleOrderListController";
import {useAfterSaleEnumManager} from "@/manager/base/AfterSaleEnumManager";
import {UnwrapNestedRefs} from "@vue/reactivity";
import {AfterSaleOrderDetailDTO} from "@/api/afterSale/order/dto/AfterSaleOrderDetailDTO";
import WaitSignEdit from './edit.vue'
import {useFormatterManager} from "@/manager/base/FormatterManager";
import AfterSaleOrderSearchRequest from "@/api/afterSale/order/request/AfterSaleOrderSearchRequest";

const detailRef = shallowRef(Detail)
const signRef = shallowRef(Sign)
const editRef = shallowRef(WaitSignEdit)

const showMore = ref(true)
const showSignRef = ref(false)
const showDetailRef = ref(false)
const showEditRef = ref(false)


/**
 * 获取售后所有配置
 */
let afterSaleOrderConfig = useAfterSaleEnumManager();
let formatterManager = useFormatterManager(afterSaleOrderConfig);

let queryFormData: UnwrapNestedRefs<AfterSaleOrderSearchRequest> = reactive(new AfterSaleOrderSearchRequest())
queryFormData.status = 'WAIT_SIGN'

/**
 * 获取分页数据
 */
const {pagerData, getPageLists, resetParams, resetOnePage} = usePagerManager({
    fetchFun: getAfterSaleListsByPage,
    params: queryFormData
})


/**
 *  处理详情页
 *
 * @author 徐寿春
 * 2023/4/6 13:45
 */
async function handleRowDetail(row: AfterSaleOrderDetailDTO) {
    showDetailRef.value = true
    await nextTick()
    detailRef.value?.open(row.afterSaleOrderId)
}


/**
 * 获取工单列表
 */
onMounted(() => {
    getPageLists()
    
})

// ------------------------------------------ ** 处理非公共部分 ** ------------------------------------------------------------------------------------


/**
 * 处理单个工单确认
 *
 * @author 徐寿春
 * 2023/4/9 07:34
 */
async function handleSignConfirm(row: any) {
    showSignRef.value = true
    await nextTick()
    signRef.value?.open(row)
}


/**
 *  处理批量信息
 *
 * @author 徐寿春
 * 2023/4/6 13:45
 */
async function handleWaitSignList() {
    showEditRef.value = true
    await nextTick()
    editRef.value?.open()
}


/**
 *  处理确认成功
 *
 * @author 徐寿春
 * 2023/4/8 23:25
 */
function handPopupConfirm() {
    getPageLists()
    
}


</script>

